package com.lpit.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lpit.constant.Constant;
import com.lpit.dto.ArticleDto;
import com.lpit.pojo.Article;
import com.lpit.pojo.Channel;
import com.lpit.pojo.User;
import com.lpit.service.impl.ArticleServiceImpl;
import com.lpit.service.impl.ChannelServiceImpl;
import com.lpit.service.impl.CommentServiceImpl;
import com.lpit.service.impl.UserServiceImpl;
import com.lpit.utils.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *     内容管理
 * <p/>
 */
@RestController
@RequestMapping("/contentManage")
@Slf4j
public class ContentManagementController {
    @Autowired
    CommentServiceImpl commentService;
    @Autowired
    ChannelServiceImpl channelService;
    @Autowired
    UserServiceImpl userService;
    @Autowired
    ArticleServiceImpl articleService;

    /**
     * 文章信息分页查询
     */
    @GetMapping("/manage/{pageNum}/{pageSize}")
    public Result<Page> page(@PathVariable Integer pageNum, @PathVariable Integer pageSize, String name, String state){
        log.info("当前页数"+pageNum);
        log.info("每页显示多少"+pageSize);
        //构造分页构造器对象
        Page<Article> pageInfo = new Page<>(pageNum,pageSize);
        //前端扩展
        Page<ArticleDto> articleDtoPage = new Page<>(pageNum,pageSize);
        //条件构造器
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件(文章名称、作者、所属频道、评论数、更新时间、状态)
        queryWrapper.like(name!=null,Article::getArticleTitle,name);
        queryWrapper.eq(state!=null,Article::getArticleStatus,state);
        //执行分页查询
        articleService.page(pageInfo,queryWrapper);
        BeanUtils.copyProperties(pageInfo,articleDtoPage);

        List<Article> articles = pageInfo.getRecords();
        List<ArticleDto> articleDtos = new ArrayList<>();
        articles.forEach(article -> {
            ArticleDto articleDto = new ArticleDto();
            BeanUtils.copyProperties(article,articleDto);
            //作者
            Integer userId =article.getUserId();
            User user = userService.getById(userId);
            articleDto.setArticleAuthor(user.getName());
            //所属频道
            Integer channelId = article.getChannelId();
            Channel channel = channelService.getById(channelId);
            articleDto.setChannelName(channel.getChannelName());
            articleDtos.add(articleDto);

        });
        articleDtoPage.setRecords(articleDtos);
        return Result.success(articleDtoPage);
    }
    /**
     * 评论数
     */
    @RequestMapping("/number/{id}")
    public Integer commentNumber(@PathVariable Integer id){
        return  commentService.commentByIdNumber(id);
    }

    /**
     * description:文章置顶
     */
    @PostMapping("/setTop/{id}")
    public Integer setTop(@PathVariable Integer id){
        return (articleService.findByIdIsTop(id));
    }

    /**
     * 封禁文章,更新文章发布状态
     */
    @ApiOperation("更改发布状态")
    @PutMapping("/forbid/{id}")
    public Result<String> forbidArticle(@PathVariable Integer id){
        Article article = articleService.getById(id);
        article.setArticleAuditStatus(Constant.FORBID);
        return articleService.updateById(article)?
                Result.success(Constant.ARTICLE_STATUS_MODIFICATION):Result.error(Constant.ARTICLE_STATUS_FAIL);
    }

    /**
     * 删除文章
     */
    @ApiOperation("文章删除")
    @DeleteMapping("/{id}")
    public Result<String> deleteArticle(@PathVariable Integer id) {
        return articleService.removeById(id)?
                Result.success(Constant.ARTICLE_DELETED_SUCCESSFULLY):Result.error(Constant.ARTICLE_DELETED_FAIL);
    }
}
